package ldk.util.adapterwrappers.listviews;

import android.database.DataSetObserver;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import ldk.util.adapterwrappers.LoadMoreAdapterWrapper;
import ldk.util.adapterwrappers.LoadMoreViewHolder;

/**
 * 为了兼容ListView,建议使用RecyclerView
 * Created by ldkxingzhe@163.com on 2017/5/18.
 */
public class ListViewLoadMoreAdapterWrapper extends BaseAdapter{
    @SuppressWarnings("unused")
    private static final String TAG = "loadMore";

    private final BaseAdapter mRealBaseAdapter;

    private boolean mIsRefreshing = false;
    private boolean mIsEndRefreshing = false;

    private boolean mShowLoadMore = true;

    private RecyclerView.ViewHolder mLoadMoreViewHolder;
    private LoadMoreAdapterWrapper.LoadMoreListener mLoadMoreListener;

    public ListViewLoadMoreAdapterWrapper(BaseAdapter realBaseAdapter) {
        this.mRealBaseAdapter = realBaseAdapter;
        mRealBaseAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                notifyDataSetChanged();
            }
        });
    }

    public void setLoadMoreListener(@Nullable LoadMoreAdapterWrapper.LoadMoreListener listener){
        mLoadMoreListener = listener;
    }

    public void setLoadMoreViewHolder(RecyclerView.ViewHolder viewHolder){
        mLoadMoreViewHolder = viewHolder;
    }


    @Override
    public int getCount() {
        int realItemCount = mRealBaseAdapter.getCount();
        return mShowLoadMore ? realItemCount + 1 : realItemCount;
    }

    @Override
    public Object getItem(int position) {
        if (isLoadMorePosition(position)){
            return "load_more";
        }
        return mRealBaseAdapter.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        if (isLoadMorePosition(position)) return 0;
        return mRealBaseAdapter.getItemId(position);
    }

    @Override
    public int getViewTypeCount() {
        return mRealBaseAdapter.getViewTypeCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (isLoadMorePosition(position)) return 0;
        return mRealBaseAdapter.getItemViewType(position) + 1;
    }

    public void setIsRefreshing(boolean isRefreshing){
        mIsRefreshing = isRefreshing;
    }

    public void setEndRefreshing(boolean endRefreshing){
        mIsEndRefreshing = endRefreshing;
        if (mLoadMoreViewHolder instanceof  LoadMoreViewHolder){
            ((LoadMoreViewHolder) mLoadMoreViewHolder).setState(
                    endRefreshing ? LoadMoreViewHolder.NO_MORE : LoadMoreViewHolder.LOADING);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isLoadMorePosition(position)){
            if (mLoadMoreViewHolder == null){
                mLoadMoreViewHolder = new LoadMoreViewHolder(parent);
            }
            convertView = mLoadMoreViewHolder.itemView;
            if (!mIsRefreshing && !mIsEndRefreshing && mLoadMoreListener != null){
                mIsRefreshing = true;
                mLoadMoreListener.onLoadMore();
            }
        }else{
            convertView = mRealBaseAdapter.getView(position, convertView, parent);
        }
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return mRealBaseAdapter.hasStableIds();
    }

    private boolean isLoadMorePosition(int position){
        return position == mRealBaseAdapter.getCount();
    }
}
